<template>
  <div>
    <div>
      <h2>我的信息</h2>
      <div class="myInfo">
        <div>
          <el-upload
            class="upload-demo"
            ref="uploadPic"
            action="/utils/upload"
            accept=".jpeg,.jpg,.png"
            :http-request="handleUploadFile"
            :file-list="fileList"
            :limit="1"
            :on-success="handelSuccess"
            :on-error="handelError"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="handelChange"
          >
            <img slot="trigger" :src="picture" class="headerPic" />
          </el-upload>
        </div>
        <div class="felx">
          用户昵称：
          <el-input v-model="nickName" placeholder="输入用户昵称"></el-input>
        </div>
        <div class="felx">
          城市名：
          <el-input
            v-model="city"
            placeholder="请输入城市名"
            name="city"
          ></el-input>
        </div>
        <el-button type="primary">保存</el-button>
      </div>
    </div>
    <div>
      <h2>修改密码</h2>
      <div class="myInfo">
        <div class="felx">
          原密码：
          <el-input
            v-model="updatePwd.oldPwd"
            show-password
            auto-complete="new-password"
            placeholder="请输入原密码"
          ></el-input>
        </div>
        <div class="felx">
          新密码：
          <el-input
            v-model="updatePwd.newPwd"
            auto-complete="new-password"
            show-password
            placeholder="输入新密码"
          ></el-input>
        </div>
        <div class="felx">
          重复密码：
          <el-input
            v-model="updatePwd.refPwd"
            auto-complete="new-password"
            show-password
            @blur="verPassword"
            placeholder="重复密码"
          ></el-input>
        </div>
        <div>{{ verPasswordTips ? '两次密码不一致' : '' }}</div>
        <el-button type="primary" @click="handelUpdatePwd">确认修改</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getSetting, updatePasswordByEmail } from '@/request/user'
import { mapState } from 'vuex'
import axios from '@/request/http'
export default {
  data() {
    return {
      fileList: [],
      nickName: '',
      city: '',
      picture: require('@/assets/images/1552544248916310.jpg'),
      updatePwd: {
        oldPwd: '',
        newPwd: '',
        refPwd: '',
      },
      verPasswordTips: false,
    }
  },
  created() {
    this.nickName = this.userInfo.user.userInfo.regName
    // getSetting().then((res) => {
    // })
  },
  computed: {
    ...mapState({
      userInfo: (state) => {
        return state
      },
    }),
  },
  methods: {
    // 文件状态改变时
    handelChange(file, fileList) {
      this.$refs.uploadPic.submit()
    },
    handleUploadFile() {
      const params = this.fileList[0]
      // upload(this.fileList[0]).then((res) => {
      // })
    },
    // 上传成功
    handelSuccess(response, file, fileList) {
      if (response.errno == 10005) {
        this.$message({
          message: response.message,
          type: 'error',
        })
        // this.$router.push({ name: 'Login' })
      }
    },
    // 上传失败
    handelError(err, file, fileList) {
      this.$message({
        message: err,
        type: 'error',
      })
    },
    // 验证两次输入的密码是否一致
    verPassword() {
      this.verPasswordTips = !(this.updatePwd.newPwd == this.updatePwd.refPwd)
    },
    handelUpdatePwd() {
      if (this.updatePwd.newPwd !== this.updatePwd.refPwd) {
        this.verPasswordTips = true
        return
      }
      if (this.updatePwd.oldPwd == this.updatePwd.newPwd) {
        this.$message({
          message: '新密码与不能与密码一致!',
          type: 'warning',
        })
      }
      const params = {
        password: this.updatePwd.oldPwd,
        newPassword: this.updatePwd.newPwd,
      }
      updatePasswordByEmail(params).then((res) => {
        if (res.errno === 0) {
          this.$message({
            message: '修改成功',
            type: 'success',
          })
          this.updatePwd = {
            oldPwd: '',
            newPwd: '',
            refPwd: '',
          }
        } else {
          this.$message({
            message: res.message,
            type: 'error',
          })
        }
      })
    },
  },
}
</script>

<style lang="stylus">
.myInfo {
  padding-left: 20px;
  width: 400px;

  .headerPic {
    object-fit: cover;
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  .felx {
    margin: 15px 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-input {
      width: 75%;
    }
  }
}
</style>